<?php
namespace app\admin\controller;
use think\Controller;
class Cat extends Base
{
	private $obj;
    public function _initialize(){
        $this->obj=model('cat');
    }
    /**
     * 类别首页
     */
    public function index()
    {
    	$list=$this->obj->getcat();
    	// var_dump($list);exit;
        return $this->fetch('',['list'=>$list]);
    }
    /**
     * 类别添加
     */
    public function add(){
    	if (request()->isPost()) {
            if(input('post.cat_id')==-1 ){
                return $this->error('请选择保险种类');
            }
            $validate = validate('cat');
    		
    		$data['cat_name']=input('post.cat_name');
            $data['parent_id']=input('post.cat_id');
    		$data['create_time']=time();
           
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
            //添加数据
			if($this->obj->add($data)){
			    $this->success("添加类别成功",'cat/index');
			}
    	}
        $cats=$this->obj->where('parent_id',0)->select();
    	return $this->fetch('',['cats'=>$cats]);
    }
    /**
     * 类别删除
     */
    public function del(){
    	$id=input('cat_id');
    	// var_dump($id);exit;
    	if($this->obj->where('cat_id',$id)->delete()){
    		$this->success("批量删除成功",'cat/index');
    	}
    }
    /**
     * 类别编辑
     */
    public function edit(){
        $cat_id=input('cat_id');
        // var_dump($cat_id);exit;
        if (request()->isPost()) {
            $data['cat_name']=input('post.cat_name');
            $data['create_time']=time();
            $cat_id=input('cat_id');
            // var_dump($cat_id);exit;
            if($this->obj->where('cat_id',$cat_id)->update($data)){
                $this->success("修改类别成功",'cat/index');
            }
        }  
        $cat_name=$this->obj->where('cat_id',$cat_id)->value('cat_name');
        return $this->fetch('',['cat_name'=>$cat_name,'cat_id'=>$cat_id]);
    }
}
